package org.lendoo.web.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.lendoo.web.pojo.common.ApiResult;
import org.lendoo.web.pojo.common.ServiceResult;
import org.lendoo.web.util.IpAddressUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import org.lendoo.web.entity.InterfaceCallLog;
import org.lendoo.web.pojo.vo.InterfaceCallLogVo;
//import org.lendoo.web.controller.AbstractController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**
 * InterfaceCallLog的controller类
 */
@Api(value = "InterfaceCallLog的controller类", tags = "InterfaceCallLogController")
@RestController
@RequestMapping("api/v1/interfaceCallLog")
@Slf4j
public class InterfaceCallLogController extends AbstractController {

    /**
     * 新增
     * @param interfaceCallLogVo InterfaceCallLog的dto类
     * @param request
     * @param response
     * @return
     */
    @ApiOperation("新增")
    @ApiImplicitParam(name = "interfaceCallLogDto", value = "InterfaceCallLog的dto类", required = true, dataType = "InterfaceCallLogDto")
    @RequestMapping(value = "/add", method = RequestMethod.POST, produces = {"application/json; charset=utf-8"})
    @ResponseBody
    public ResponseEntity<ApiResult<Boolean>> add(@RequestBody InterfaceCallLogVo interfaceCallLogVo, HttpServletRequest request, HttpServletResponse response) {
        ApiResult apiResult = new ApiResult();
        if (null == interfaceCallLogVo) {
            String message = "调用接口【/api/v1/interfaceCallLog/add时，参数不合法";
            log.warn(message);
            apiResult.setSuccess(Boolean.FALSE);
            apiResult.setCode(HttpStatus.BAD_REQUEST.value());
            apiResult.setMessage(message);
            return new ResponseEntity<>(apiResult, HttpStatus.BAD_REQUEST);
        }

        InterfaceCallLog interfaceCallLog = new InterfaceCallLog();
        BeanUtils.copyProperties(interfaceCallLogVo, interfaceCallLog);
        interfaceCallLog.setCreateTime(new Date());
        interfaceCallLog.setRemoteAddress(IpAddressUtil.getIpAddress(request));
        ServiceResult<Boolean> serviceResult = interfaceCallLogService.add(interfaceCallLog);

        apiResult.setSuccess(serviceResult.getSuccess());
        apiResult.setCode(HttpStatus.OK.value());
        apiResult.setMessage(serviceResult.getMessage());
        apiResult.setResult(serviceResult.getResult());
        return new ResponseEntity<>(apiResult, HttpStatus.OK);
    }

//    /**
//     * 批量导入
//     * @param interfaceCallLogDtoList InterfaceCallLog的dto类列表
//     * @param request
//     * @param response
//     * @return
//     */
//    @ApiOperation("批量导入")
//    @ApiImplicitParam(name = "interfaceCallLogDtoList", value = "InterfaceCallLog的dto类列表", required = true, dataType = "List")
//    @RequestMapping(value = "/batchAdd", method = RequestMethod.POST, produces = {"application/json; charset=utf-8"})
//    @ResponseBody
//    public ResponseEntity<ApiResult<Boolean>> batchAdd(@RequestBody List<InterfaceCallLogDto> interfaceCallLogDtoList, HttpServletRequest request, HttpServletResponse response) {
//        ApiResult apiResult = new ApiResult();
//        if (null == interfaceCallLogDtoList) {
//            String message = "调用接口【/api/v1/interfaceCallLog/batchAdd时，参数不合法";
//            log.warn(message);
//            apiResult.setSuccess(Boolean.FALSE);
//            apiResult.setCode(HttpStatus.BAD_REQUEST.value());
//            apiResult.setMessage(message);
//            return new ResponseEntity<>(apiResult, HttpStatus.BAD_REQUEST);
//        }
//
//        List<InterfaceCallLog> interfaceCallLogList = new ArrayList<>();
//        BeanUtils.copyProperties(interfaceCallLogDtoList, interfaceCallLogList);
//        ServiceResult<Boolean> serviceResult = interfaceCallLogService.batchAdd(interfaceCallLogList);
//
//        apiResult.setSuccess(serviceResult.getSuccess());
//        apiResult.setCode(HttpStatus.OK.value());
//        apiResult.setMessage(serviceResult.getMessage());
//        apiResult.setResult(serviceResult.getResult());
//        return new ResponseEntity<>(apiResult, HttpStatus.OK);
//    }
//
//    /**
//     * 根据id更新
//     * @param interfaceCallLogDto InterfaceCallLog的dto类
//     * @param request
//     * @param response
//     * @return
//     */
//    @ApiOperation("根据id更新")
//    @ApiImplicitParam(name = "interfaceCallLogDto", value = "InterfaceCallLog的dto类", required = true, dataType = "InterfaceCallLogDto")
//    @RequestMapping(value = "/updateForId", method = RequestMethod.POST, produces = {"application/json; charset=utf-8"})
//    @ResponseBody
//    public ResponseEntity<ApiResult<Boolean>> updateForId(@RequestBody InterfaceCallLogDto interfaceCallLogDto, HttpServletRequest request, HttpServletResponse response) {
//        ApiResult apiResult = new ApiResult();
//        if (null == interfaceCallLogDto) {
//            String message = "调用接口【/api/v1/interfaceCallLog/updateForId时，参数不合法";
//            log.warn(message);
//            apiResult.setSuccess(Boolean.FALSE);
//            apiResult.setCode(HttpStatus.BAD_REQUEST.value());
//            apiResult.setMessage(message);
//            return new ResponseEntity<>(apiResult, HttpStatus.BAD_REQUEST);
//        }
//
//        InterfaceCallLog interfaceCallLog = new InterfaceCallLog();
//        BeanUtils.copyProperties(interfaceCallLogDto, interfaceCallLog);
//        ServiceResult<Boolean> serviceResult = interfaceCallLogService.updateForId(interfaceCallLog);
//
//        apiResult.setSuccess(serviceResult.getSuccess());
//        apiResult.setCode(HttpStatus.OK.value());
//        apiResult.setMessage(serviceResult.getMessage());
//        apiResult.setResult(serviceResult.getResult());
//        return new ResponseEntity<>(apiResult, HttpStatus.OK);
//    }
//
//    /**
//     * 根据id删除
//     * @param id 主键
//     * @param request
//     * @param response
//     * @return
//     */
//    @ApiOperation("根据id删除")
//    @ApiImplicitParam(name = "id", value = "主键", required = true, dataType = "Integer")
//    @RequestMapping(value = "/deleteById/{id}", method = RequestMethod.GET, produces = {"application/json; charset=utf-8"})
//    @ResponseBody
//    public ResponseEntity<ApiResult<Boolean>> deleteById(@PathVariable("id") Integer id, HttpServletRequest request, HttpServletResponse response) {
//        ApiResult apiResult = new ApiResult();
//        if (null == id) {
//            String message = "调用接口【/api/v1/interfaceCallLog/deleteById时，参数不合法";
//            log.warn(message);
//            apiResult.setSuccess(Boolean.FALSE);
//            apiResult.setCode(HttpStatus.BAD_REQUEST.value());
//            apiResult.setMessage(message);
//            return new ResponseEntity<>(apiResult, HttpStatus.BAD_REQUEST);
//        }
//
//        ServiceResult<Boolean> serviceResult = interfaceCallLogService.deleteById(id);
//
//        apiResult.setSuccess(serviceResult.getSuccess());
//        apiResult.setCode(HttpStatus.OK.value());
//        apiResult.setMessage(serviceResult.getMessage());
//        apiResult.setResult(serviceResult.getResult());
//        return new ResponseEntity<>(apiResult, HttpStatus.OK);
//    }
//
//    /**
//     * 根据id查询
//     * @param id 主键
//     * @param request
//     * @param response
//     * @return
//     */
//    @ApiOperation("根据id查询")
//    @ApiImplicitParam(name = "id", value = "主键", required = true, dataType = "Integer")
//    @RequestMapping(value = "/selectById/{id}", method = RequestMethod.GET, produces = {"application/json; charset=utf-8"})
//    @ResponseBody
//    public ResponseEntity<ApiResult<InterfaceCallLog>> selectById(@PathVariable("id") Integer id, HttpServletRequest request, HttpServletResponse response) {
//        ApiResult apiResult = new ApiResult();
//        if (null == id) {
//            String message = "调用接口【/api/v1/interfaceCallLog/selectById时，参数不合法";
//            log.warn(message);
//            apiResult.setSuccess(Boolean.FALSE);
//            apiResult.setCode(HttpStatus.BAD_REQUEST.value());
//            apiResult.setMessage(message);
//            return new ResponseEntity<>(apiResult, HttpStatus.BAD_REQUEST);
//        }
//
//        ServiceResult<InterfaceCallLog> serviceResult = interfaceCallLogService.selectById(id);
//
//        apiResult.setSuccess(Boolean.TRUE);
//        apiResult.setCode(HttpStatus.OK.value());
//        apiResult.setMessage(serviceResult.getMessage());
//        apiResult.setResult(serviceResult.getResult());
//        return new ResponseEntity<>(apiResult, HttpStatus.OK);
//    }
//
//    /**
//     * 根据条件，分页查询，升序/降序排列
//     * @param interfaceCallLogDto InterfaceCallLog的dto类
//     * @param request
//     * @param response
//     * @return
//     */
//    @ApiOperation("根据条件，分页查询，升序/降序排列")
//    @ApiImplicitParam(name = "interfaceCallLogDto", value = "InterfaceCallLog的dto类", required = true, dataType = "InterfaceCallLogDto")
//    @RequestMapping(value = "/page", method = RequestMethod.POST, produces = {"application/json; charset=utf-8"})
//    @ResponseBody
//    public ResponseEntity<ApiResult<PageInfo<InterfaceCallLog>>> page(@RequestBody InterfaceCallLogDto interfaceCallLogDto, HttpServletRequest request, HttpServletResponse response) {
//        ApiResult apiResult = new ApiResult();
//        if (null == interfaceCallLogDto) {
//            String message = "调用接口【/api/v1/interfaceCallLog/page时，参数不合法";
//            log.warn(message);
//            apiResult.setSuccess(Boolean.FALSE);
//            apiResult.setCode(HttpStatus.BAD_REQUEST.value());
//            apiResult.setMessage(message);
//            return new ResponseEntity<>(apiResult, HttpStatus.BAD_REQUEST);
//        }
//        if (null == interfaceCallLogDto.getPageNo() || null == interfaceCallLogDto.getPageSize()) {
//            String message = "调用接口【/api/v1/interfaceCallLog/page时，参数pageNo和pageSize不能为空";
//            log.warn(message);
//            apiResult.setSuccess(Boolean.FALSE);
//            apiResult.setCode(HttpStatus.BAD_REQUEST.value());
//            apiResult.setMessage(message);
//            return new ResponseEntity<>(apiResult, HttpStatus.BAD_REQUEST);
//        }
//
//        InterfaceCallLog interfaceCallLog = new InterfaceCallLog();
//        BeanUtils.copyProperties(interfaceCallLogDto, interfaceCallLog);
//        ServiceResult<PageInfo<InterfaceCallLog>> serviceResult = interfaceCallLogService.page(interfaceCallLog, interfaceCallLogDto.getPageNo(), interfaceCallLogDto.getPageSize());
//
//        apiResult.setSuccess(Boolean.TRUE);
//        apiResult.setCode(HttpStatus.OK.value());
//        apiResult.setMessage(serviceResult.getMessage());
//        apiResult.setResult(serviceResult.getResult());
//        return new ResponseEntity<>(apiResult, HttpStatus.OK);
//    }
}
